package com.ticket.sass.common.entity.tenant;

import com.baomidou.mybatisplus.annotation.*;
import com.ticket.sass.common.entity.tenant.enums.CouponActivateType;
import com.ticket.sass.common.entity.tenant.enums.CouponAvailablePeriod;
import com.ticket.sass.common.entity.tenant.enums.CouponScene;
import com.ticket.sass.common.entity.tenant.enums.CouponType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 优惠券表
 * </p>
 *
 * @author ywb
 * @since 2024-03-13
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("coupons")
@ApiModel(value = "CouponsEntity对象", description = "优惠券表")
public class CouponEntity implements Serializable {
    @ApiModelProperty(value = "优惠券ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "券名称")
    private String name;

    @ApiModelProperty(value = "券类型：抵扣券，满减券，折扣券")
    private CouponType type;

    @ApiModelProperty(value = "使用场景：影票，卖品")
    private CouponScene scene;

    @TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    @ApiModelProperty(value = "优惠值：抵扣券 - 忽略，满减券 - 表示金额，折扣券 - 表示折扣")
    private BigDecimal value;

    @TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    @ApiModelProperty(value = "门槛值：抵扣券 - 忽略，满减券 - 表示满多少可用，折扣券 - 表示满多少可用")
    private BigDecimal thresholdValue;

    @ApiModelProperty(value = "生效类型：指定日期范围，领取后指定")
    private CouponActivateType activateType;

    @TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    @ApiModelProperty(value = "可用开始日期（activateType=指定日期范围 时使用）")
    private LocalDate startDate;

    @TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    @ApiModelProperty(value = "可用结束日期（activateType=指定日期范围 时使用）")
    private LocalDate endDate;

    @TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    @ApiModelProperty(value = "领取后 N 天开始生效，0 表示立即生效（activateType=领取后指定 时使用）")
    private Integer activateStart;

    @TableField(insertStrategy = FieldStrategy.NOT_EMPTY, updateStrategy = FieldStrategy.NOT_EMPTY)
    @ApiModelProperty(value = "生效后 N 天内有效，-1 表示可用日期内有效（activateType=领取后指定 时使用）")
    private Integer activateEnd;

    @ApiModelProperty(value = "可用时段：不限制，指定范围")
    private CouponAvailablePeriod availablePeriod;

    @ApiModelProperty(value = "是否允许会员卡支付时使用")
    private Boolean allowCard;

    @ApiModelProperty(value = "仅限新用户")
    private Boolean newUserOnly;

    @ApiModelProperty(value = "领取统计")
    private Integer receiveCount;

    @ApiModelProperty(value = "使用说明")
    @TableField("\"desc\"")
    private String desc;

    @ApiModelProperty(value = "影院 id")
    private Long cinemaId;

    @ApiModelProperty(value = "租户 id")
    private Long tenantId;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    public LocalDateTime createdAt;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    public LocalDateTime updatedAt;

    @TableField(select = false)
    @ApiModelProperty(value = "删除时间")
    @TableLogic(value = "null", delval = "now()")
    public LocalDateTime deletedAt;

    @TableField(select = false)
    private List<CouponWeekEntity> couponWeeks;

    public LocalDateTime[] calculateTimeRange() {
        var validStartTime = LocalDateTime.now();
        var validEndTime = LocalDateTime.now();
        switch (activateType) {
            case SET_DATE:
                validStartTime = startDate.atTime(0, 0);
                validEndTime = endDate.atTime(0, 0);
                break;
            case AFTER_DATE:
                validStartTime = LocalDateTime.now().plusDays(activateStart);
                if (activateEnd == -1) { // -1 表示可用日期内有效
                    validEndTime = endDate.atTime(0, 0);
                } else {
                    validEndTime = validStartTime.plusDays(activateEnd);
                }
                break;
        }
        return new LocalDateTime[]{validStartTime, validEndTime};
    }
}
